package com.swift.widget;


import java.io.File;
import java.util.Map;
import java.util.Set;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.AttributeSet;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.swift.utils.DialogTool;

public class CommonWebView extends WebView{
	private String url = "";//访问基本路径
	private Map<String, Object> map;
	private Dialog dialog;
	private static final String TAG = "CommonWebView";  
	public CommonWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initWebView(context);
	}
	
	@SuppressLint("NewApi")
	private void initWebView(Context ctx) {

		this.getSettings().setDefaultTextEncodingName("UTF-8");

		this.getSettings().setJavaScriptEnabled(true);
		this.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		// 开启 DOM storage API 功能
		this.getSettings().setDomStorageEnabled(true);
		// 开启 database storage API 功能
		this.getSettings().setDatabaseEnabled(true);
		String cacheDirPath = ctx.getFilesDir().getAbsolutePath()+"/WebCache";
//		String cacheDirPath = APP_CACAHE_DIRNAME;
//		Log.e(TAG,cacheDirPath);
		
		
		 if (Environment.getExternalStorageState().equals(
		 Environment.MEDIA_MOUNTED)) {
		 File dir = new File(cacheDirPath);
		 if (!dir.exists()) {
		 dir.mkdirs();
		 }}
		
		// String cacheDirPath =
		// getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
		 this.getSettings().setAppCacheMaxSize(1024*1024*80);   
		// 设置数据库缓存路径
		 this.getSettings().setDatabasePath(cacheDirPath);
		// 设置 Application Caches 缓存目录
		 this.getSettings().setAppCachePath(cacheDirPath);
		// 开启 Application Caches 功能
		 this.getSettings().setAppCacheEnabled(true);
		 this.getSettings().setAllowFileAccess(true);   // 可以读取文件缓存(manifest生效)
		 this.setWebViewClient(new WebViewClient() {

			@Override 
            public void onLoadResource(WebView view, String url) { 
                super.onLoadResource(view, url); 
            } 
   
            @Override 
            public boolean shouldOverrideUrlLoading(WebView webview, String url) { 
                webview.loadUrl(url); 
                return true; 
            } 
   
            @Override 
            public void onPageStarted(WebView view, String url, Bitmap favicon) { 
//            	dialog = DialogTool.creatDialog(view.getContext());
            } 
   
            @Override 
            public void onPageFinished(WebView view, String url) { 
//            	DialogTool.close(dialog);
            } 
   
            @Override 
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
//            	DialogTool.close(dialog);
            }  
		});
		
		 this.setWebChromeClient(new WebChromeClient() { 
			   
            @Override 
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) { 
                result.confirm(); 
                return true; 
            } 
   
            @Override 
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { 
                return super.onJsConfirm(view, url, message, result); 
            } 
   
            @Override 
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { 
                return super.onJsPrompt(view, url, message, defaultValue, result); 
            } 
        });  
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
    
	
	/**
	 * author hym
	 * desc 添加js接口
	 * 2016-8-29
	 */
	public void setJsInteface()
	{
		if (map != null) {
			Set<Map.Entry<String, Object>> sets = map.entrySet();
			for (Map.Entry<String, Object> entry : sets) {
				String key = entry.getKey();
				Object value = entry.getValue();
				this.addJavascriptInterface(value, key);
			}
		}
		
	}
}
